package com.sms.platform.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sms.platform.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author xjh
 * @date 2020-08-10
 */
@RestController
@RequestMapping("/cache")
@Slf4j
public class CacheController {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping(value = "/set")
    public void save2Cache(@RequestParam("key") String key,
                           @RequestParam("value") String value,
                           @RequestParam("expireSecond") Integer expireSecond) {
        checkParams(key);
        log.info("key:{},value:{},expireSecond:{}", key, value, expireSecond);
        cacheService.set(key, value, expireSecond);
    }

    /**
     * 把key和value放入redis的String中
     * 放入的value是String类型
     */
    @PostMapping("/set/{key}/{value}")
    public boolean setString(@PathVariable String  key, @PathVariable String value){
        return cacheService.set(key,value);
    }

    /**
     * 从redis的string中读取出指定的key的值
     * 取出的value是String类型
     */
    @GetMapping("/get/{key}")
    public String getString(@PathVariable String key){
        return (String) cacheService.get(key);
    }

    /**
     * 接收前端传递过来的keys数组,然后从redis中删除keys数组中对应的key
     * 前端传递时,使用form表单格式 键值对 键:keys 值:FIRST_BLACK:13439988877, FIRST_BLACK:13437789988 多个值之间逗号间隔
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public boolean deleteKey(@RequestParam(value = "keys") String[] keys){
        cacheService.delBatch(keys);
        return true;
    }

    /**
     * 从redis的hash中读取出指定key的数据
     */
    @GetMapping("/hmget/{key}")
    public Map hmget(@PathVariable String key){
        return cacheService.hmget(key);
    }

    /**
     * 将map格式的数据放入redis的hash中
     */
    @PostMapping("/hmset/{key}")
    public boolean hmset(@PathVariable String key, @RequestBody Map map){
        return cacheService.hmset(key, map);
    }
    /**
     * 对指定key的余额进行增加
     * @param key
     * @param number 要增加的金额
     * @return
     */


    @RequestMapping("/incr/{key}/{number}")
    public long incr(@PathVariable String key,@PathVariable long number){
        if(number<=0){
            throw new RuntimeException("数字必须大于等于0");
        }
        return cacheService.incr(key,number);
    }

    /**
     * 对指定key的余额进行减操作
     * @param key
     * @param number 要减少的金额
     * @return
     */
    @RequestMapping("/decr/{key}/{number}")
    public long decr(@PathVariable String key,@PathVariable long number){
        if(number<=0){
            throw new RuntimeException("数字必须大于等于0");
        }
        return cacheService.decr(key,number);
    }

    /**
     * 获取指定key的过期时间
     */
    @RequestMapping("/expire/{key}")
    public long getExpire(@PathVariable String key){
        return cacheService.getExpire(key);
    }



    private void checkParams(String key) {
        Assert.notNull(key, "non null key required");
    }
}